<%- include("header",{type:'signin'}) %>
<div class="container">
    <form class="form create" method="post ">
        <div>
            <label>用户名：</label>
            <input placeholder="用户名" type="text" name="name">
        </div>
        <div>
            <label>密码：</label>
            <input placeholder="密码" type="password" name="password">
        </div>
        <div class="submit">登录</div>
    </form>
</div>
<script>
    $(window).keyup(function(e){
        //console.log(e.keyCode)
        if (e.keyCode == 13) {
            $('.submit').click()
        }
    })
    $('.submit').click(()=>{
        if ($('input[name=name]').val().trim() == '' || $('input[name=password]').val().trim() == '' ) {
            fade('请输入用户名或密码')
        }else{
            console.log($('.form').serialize())
            $.ajax({
                url: "/signin",
                data: $('.form').serialize(),
                type: "POST",
                cache: false,
                dataType: 'json',
                success: function (msg) {
                    if (!msg) {
                        $('input').val('')
                        fade('用户名或密码错误')
                    } else{
                        fade('登录成功')
                        setTimeout(()=>{
                            window.location.href = "/posts"
                        },1500)
                    }
                },
                error: function () {
                    alert('异常');
                }
            })
        }
    })
</script>
<% include footer %>